package com.park.web.service.impl;

import com.park.web.base.baseOperation.BaseService;
import com.park.web.dao.pbaOwnerOrganizationMapper;
import com.park.web.model.domain.pbaOwnerOrganization;
import com.park.web.model.domain.pbaParkingOrganizationInfo;
import com.park.web.model.vo.pbaOwnerOrganizationTreeVo;
import com.park.web.model.vo.pbaOwnerOrganizationVo;
import com.park.web.model.vo.pbaParkingOrganizationInVo;
import com.park.web.service.pbaOwnerOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class pbaOwnerOrganizationServiceImpl extends BaseService<pbaOwnerOrganization> implements pbaOwnerOrganizationService {

    @Autowired
    pbaOwnerOrganizationMapper ipbaOwnerOrganizationMapper;

    public boolean hasOrgCode(String orgCode) {
        return ipbaOwnerOrganizationMapper.hasOrgCode(orgCode) > 0 ? true : false;
    }

    public Integer deleteByOrgCode(String orgCode) {
        return ipbaOwnerOrganizationMapper.deleteByOrgCode(orgCode);
    }

    public List<pbaOwnerOrganizationVo> selectByParentOrgCodeTree(String parentOrganizationCode) {
        List<pbaOwnerOrganizationVo> result = new ArrayList<>();
        List<pbaOwnerOrganization> list = new ArrayList<>();
        var actionList = ipbaOwnerOrganizationMapper.selectAll();
        if (null == parentOrganizationCode || parentOrganizationCode.isBlank()) {
            list = actionList.stream().filter(x -> x.getParentOrganizationCode() == null).collect(Collectors.toList());
            actionList.removeIf(x -> x.getParentOrganizationCode() == null);
        } else {
            actionList.removeIf(x -> x.getParentOrganizationCode() == null);
            list = actionList.stream().filter(x -> x.getParentOrganizationCode().equals(parentOrganizationCode)).collect(Collectors.toList());
        }
        list.forEach(x -> {
            pbaOwnerOrganizationVo temp = new pbaOwnerOrganizationVo();
            boolean hasChildren = actionList.stream().filter(t -> t.getParentOrganizationCode().contains(x.getOrganizationCode())).count() > 0;
            temp.setHasChildren(hasChildren);
            temp.setOrganizationCode(x.getOrganizationCode());
            temp.setOrganizationName(x.getOrganizationName() + "(" + x.getOrganizationCode() + ")");
            temp.setChild(new ArrayList<>());
            result.add(temp);
        });
        return result;
    }

    public List<pbaOwnerOrganizationTreeVo> selectAllTree(String parentOrganizationCode) {
        var actionList = ipbaOwnerOrganizationMapper.selectAll();
        return this.getTree(actionList, null);
    }

    private List<pbaOwnerOrganizationTreeVo> getTree(List<pbaOwnerOrganization> list, String parentOrgCode) {
        List<pbaOwnerOrganization> listChildrens = null;
        if (parentOrgCode == null) {
            listChildrens = list.stream().filter(x -> x.getParentOrganizationCode() == parentOrgCode).collect(Collectors.toList());
        } else {
            list.removeIf(x -> x.getParentOrganizationCode() == null);
            listChildrens = list.stream().filter(x -> x.getParentOrganizationCode().equals(parentOrgCode)).collect(Collectors.toList());
        }
        List<pbaOwnerOrganizationTreeVo> listVo = new ArrayList<pbaOwnerOrganizationTreeVo>();
        for (var children : listChildrens) {
            pbaOwnerOrganizationTreeVo entity = new pbaOwnerOrganizationTreeVo();
            entity.setChecked(false);
            boolean hasChildred = parentOrgCode == null ? true : list.stream().filter(x -> x.getParentOrganizationCode().equals(children.getOrganizationCode())).count() > 0;
            entity.setHasChildren(hasChildred);
            entity.setDownChecked(false);
            entity.setId(children.getOrganizationCode());
            entity.setExpanded(true);
            entity.setFullName(children.getOrganizationName());
            entity.setModel(children);
            var tempList = getTree(list, children.getOrganizationCode());
            if (tempList.size() > 0)
                entity.setItems(tempList);
            listVo.add(entity);
        }
        return listVo;
    }


}